package com.wenhao.ctrip;

import java.util.Scanner;

/**
 * @author Wenhao Tong
 * @create 2021-09-09 19:27
 */
public class Main2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] A = new int[n];
        for (int i = 0;i < n;i++) {
            A[i] = scanner.nextInt();
        }
        System.out.println(getMin(A,0,k));
    }

    private static int getMin(int[] A,int lo,int k) {
        if (A.length - lo == k) {
            return 0;
        }
        int min = A[lo];
        int max = A[lo];
        if (k == 1) {
            for (int i = lo;i < A.length;i++) {
                min = Math.min(min,A[i]);
                max = Math.max(max,A[i]);
            }
            return max - min;
        }
        int result = Integer.MAX_VALUE;
        for (int i = lo;i <= A.length - k;i++) {
            min = Math.min(min,A[i]);
            max = Math.max(max,A[i]);
            result = Math.min(getMin(A,i + 1,k - 1) + max - min,result);
        }
        return result;
    }
}
